package com.oocl.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.oocl.dao.MerchantDao;
import com.oocl.factory.DBConFactory;
import com.oocl.log4j.Log4j;
import com.oocl.pojo.Merchant;

public class MerchantDaoImpl implements MerchantDao {
	private static final Logger logger = LogManager
			.getLogger(MerchantDaoImpl.class);

	@Override
	public Merchant findByName(String uname) {
		String sql = "select * from merchant where uname = ?";
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		Merchant merchant = null;
		try {
			con = DBConFactory.getConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1, uname);
			rs = pst.executeQuery();
			while (rs.next()) {
				String id = rs.getString("id");
				String password = rs.getString("password");
				String headImg = rs.getString("head_img");
				String idcard = rs.getString("idcard");
				int status = rs.getInt("status");
				String address = rs.getString("address");
				String content = rs.getString("content");
				merchant = new Merchant(id, uname, password, headImg, idcard,
						status, address, content);
				
			}
		} catch (SQLException e) {
			Log4j.getLogger().error("[ 由 uname 查找Merchant出错 ] {}", e.fillInStackTrace());
		}finally{
			DBConFactory.close(con, pst, rs);
		}
		if (merchant != null) {
			logger.debug("merchant find id {} success ", uname);
		} else {
			logger.debug("merchant find id {} fail ", uname);
		}
		return merchant;
	}

}
